<script setup lang="ts">
import { ref } from "vue";
import { useRole } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { regionData, provinceAndCityData } from "@/utils/chinaArea";

import Eye from "~icons/ri/Eye-line";
import Refresh from "~icons/ep/refresh";

import CustomerDetail from "@/components/customerDetail/index.vue"

defineOptions({
  name: "CustomerDataConversionPool"
});

const formRef = ref();
const detailRef = ref();
const {
  permissions,

  shortcuts,
  labelList,
  sourceList,
  userList,
  statusList,
  form,
  loading,
  columns,
  dataList,
  pagination,

  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  lookCustomer,
} = useRole(detailRef);
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item prop="keyword">
        <el-input
          v-model="form.keyword"
          placeholder="客户名/企业名/手机号"
          clearable
          class="w-[180px]!"
        />
      </el-form-item>
      <el-form-item prop="tag_id">
        <el-select
          v-model="form.tag_id"
          placeholder="客户标签"
          clearable
          class="w-[120px]!"
        >
          <el-option v-for="(item, index) in labelList" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="address">
        <el-cascader
          v-model="form.address"
          placeholder="所在地区"
          :options="regionData"
          :props="{ checkStrictly: true, value: 'label' }"
          clearable
          class="w-[120px]!"
        />
      </el-form-item>
      <el-form-item prop="source_by">
        <el-select
          v-model="form.source_by"
          placeholder="数据来源"
          clearable
          class="w-[120px]!"
        >
          <el-option v-for="(item, index) in sourceList" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="转移/领取时间" prop="updated_at">
        <el-date-picker
          v-model="form.updated_at"
          type="daterange"
          class="w-[240px]!"
          unlink-panels
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :shortcuts="shortcuts"
        />
      </el-form-item>
      <el-form-item label="最近跟进时间" prop="called_at">
        <el-date-picker
          v-model="form.called_at"
          type="daterange"
          class="w-[240px]!"
          unlink-panels
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :shortcuts="shortcuts"
        />
      </el-form-item>
      <el-form-item prop="status">
        <el-select
          v-model="form.status"
          placeholder="客户状态"
          clearable
          class="w-[120px]!"
        >
          <el-option v-for="(item, index) in statusList" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="target_staff_id">
        <el-select
          v-model="form.target_staff_id"
          placeholder="跟进人"
          clearable
          class="w-[120px]!"
        >
          <el-option v-for="(item, index) in userList" :key="index" :label="item.nickname" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="permissions.is_cd_zh_table">
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch"
        >
          查询
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title=""
      :columns="columns"
      @refresh="onSearch"
    >
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(Eye)"
              @click="lookCustomer(row)"
            >
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>

    <CustomerDetail ref="detailRef"></CustomerDetail>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 12px;
}

.main {
  margin: 12px 12px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
